export const request = (params) => {
  const url = 'http://localhost:51601'
  const token = wx.getStorageSync('token')
  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      url: url + params.url,
      header: {
        'Authorization': wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.statusCode == 401) {
          if (token) {
            wx.showModal({
              title: '提示',
              content: '登录信息已过期，请重新登录',
              complete: (res) => {
                if (res.cancel) {}
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/user/user',
                  })
                }
              }
            })
          } else {
            wx.showModal({
              title: '未登录提示',
              content: '请登录',
              complete: (res) => {
                if (res.cancel) {}
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/user/user',
                  })
                }
              }
            })
          }
        } else if (res.statusCode == 500) {
          wx.showToast({
            title: '网络繁忙',
            icon: 'error',
            duration: 3000
          })
        } else {
          resolve(res)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}